package com.ptu.spzx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ptu.spzx.common.exception.ptuException;
import com.ptu.spzx.domain.Class.Classes;
import com.ptu.spzx.domain.Class.ClassesCourse;
import com.ptu.spzx.domain.Class.dto.ClassesDTO;
import com.ptu.spzx.domain.Delete.DeleteIds;
import com.ptu.spzx.feign.userInfo.ManagerFeignClient;
import com.ptu.spzx.mapper.ClassesCourseMapper;
import com.ptu.spzx.mapper.ExamClassMapper;
import com.ptu.spzx.model.entity.system.SysDept;
import com.ptu.spzx.model.entity.system.SysUser;
import com.ptu.spzx.model.vo.common.ResultCodeEnum;
import com.ptu.spzx.service.IClassesCourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author YT
 * @since 2024-01-28
 */
@Service
@Slf4j
public class ClassesCourseServiceImpl extends ServiceImpl<ClassesCourseMapper, ClassesCourse> implements IClassesCourseService {
    @Autowired
    ClassesCourseMapper classesCourseMapper;
    @Autowired
    private ExamClassMapper examClassMapper;
    @Autowired
    private ManagerFeignClient managerFeignClient;


    @Override
    @Transactional
    public void addClass(ClassesDTO classesDTO) {
        SysDept deptFeign = managerFeignClient.findDeptFeign(classesDTO.getClassId());
        String deptName = deptFeign.getDeptName();
        List<String> stuIds = classesDTO.getStuIds();
        //判断学生是否存在班级
        if(stuIds!=null&&stuIds.size()!=0){
            for (String stuId:stuIds){
                SysUser stu = managerFeignClient.getStu(stuId);
                if(stu.getClassName()!=null){
                    throw new ptuException(ResultCodeEnum.DATA_EXIST);
                }
            }
        }
        for (Long courseId : classesDTO.getCourseIds()) {
            ClassesCourse classesCourse = new ClassesCourse();
            classesCourse.setClassesId(classesDTO.getClassId());
            classesCourse.setBelongCourseId(courseId);
            classesCourseMapper.insert(classesCourse);
        }
        if(stuIds!=null&&stuIds.size()!=0){
            managerFeignClient.setClassId(stuIds, deptName);
        }
    }

    @Override
    public PageInfo<SysDept> findByPage(Integer pageNum, Integer pageSize, String keyWord, Long courseId) {
        List<Long> classIds=null;
        if(courseId!=null){
            classIds = classesCourseMapper.selectClassIdByCourseId(courseId);
        }
        //查出班级
        System.out.println("============");
        System.out.println(classIds);
        PageInfo<SysDept> byPage = managerFeignClient.findByPage(pageNum, pageSize, keyWord, classIds);
        return byPage;
    }

    @Override
    public PageInfo<SysUser> findAllStuByClass(List<Long> classIds, Integer pageNum, Integer pageSize) {
        if(classIds==null){
            throw new ptuException(ResultCodeEnum.PARAM_NULL);
        }
        List<String> className=new ArrayList<>();
        for(Long classId:classIds){
            SysDept deptFeign = managerFeignClient.findDeptFeign(classId);
            if(deptFeign==null){
                throw new ptuException(ResultCodeEnum.DATA_NOT_EXIST);
            }
            String deptName = deptFeign.getDeptName();
            className.add(deptName);
        }
        PageInfo<SysUser> stuListByClassName = managerFeignClient.getStuListByClassName(className, pageNum, pageSize);
        return stuListByClassName;
    }

    @Override
    public PageInfo<SysUser> findAllStuByCourse(List<Long> courseIds, Integer pageNum, Integer pageSize) {
        if(courseIds==null){
            throw new ptuException(ResultCodeEnum.PARAM_NULL);
        }
        List<Long> AllClassId=new ArrayList<>();
        for(Long courseId:courseIds){
            List<Long> classIds=classesCourseMapper.selectClassIdByCourseId(courseId);
            if(classIds==null){
                throw new ptuException(ResultCodeEnum.DATA_ERROR);
            }
            AllClassId.addAll(classIds);
        }
        List<String> className=new ArrayList<>();
        for(Long classId:AllClassId){
            SysDept deptFeign = managerFeignClient.findDeptFeign(classId);
            String deptName = deptFeign.getDeptName();
            className.add(deptName);
        }
        PageInfo<SysUser> stuListByClassName = managerFeignClient.getStuListByClassName(className, pageNum, pageSize);
        return stuListByClassName;
    }

    @Override
    public PageInfo<SysUser> findAllStuByExam(Long examId, Integer pageNum, Integer pageSize) {
        if(examId==null){
            throw new ptuException(ResultCodeEnum.PARAM_NULL);
        }
        List<Long> classIds=examClassMapper.selectByExamId(examId);
        List<String> className=new ArrayList<>();
        for(Long classId:classIds){
            SysDept deptFeign = managerFeignClient.findDeptFeign(classId);
            if(deptFeign==null){
                throw new ptuException(ResultCodeEnum.DATA_NOT_EXIST);
            }
            String deptName = deptFeign.getDeptName();
            className.add(deptName);
        }
        PageInfo<SysUser> stuListByClassName = managerFeignClient.getStuListByClassName(className, pageNum, pageSize);
        return stuListByClassName;
    }

    @Override
    public PageInfo<SysDept> getClassByCourse(Long courseId, Integer pageNum, Integer pageSize) {
        List<Long> classIds = classesCourseMapper.selectClassIdByCourseId(courseId);
        PageInfo<SysDept> byPage = managerFeignClient.findByPage(pageNum, pageSize, null, classIds);
        return byPage;
    }

    @Override
    @Transactional
    public void updateClass(ClassesDTO classesDTO) {
        SysDept deptFeign = managerFeignClient.findDeptFeign(classesDTO.getClassId());
        String deptName = deptFeign.getDeptName();
        managerFeignClient.setClassNull(deptName);
        classesCourseMapper.deleteByClassId(classesDTO.getClassId());
        if(classesDTO.getCourseIds()!=null){
            for(Long courseId:classesDTO.getCourseIds()){
                ClassesCourse classesCourse = new ClassesCourse();
                classesCourse.setClassesId(classesDTO.getClassId());
                classesCourse.setBelongCourseId(courseId);
                classesCourseMapper.insert(classesCourse);
            }
        }
        if(classesDTO.getStuIds()!=null){
            List<String> stuIds = classesDTO.getStuIds();
            managerFeignClient.setClassId(stuIds, deptName);
        }
    }


    /**
     * 删除班级，同时删除班级课程关系
     *
     * @param
     * @return
     */
    @Override
    @Transactional //如果失败，回滚事务
    public void deleteBatchClassByIds(DeleteIds deleteIds) {
        List<Long> ids = deleteIds.getIds();
        if (ids.isEmpty()) {
            throw new ptuException(ResultCodeEnum.DATA_ERROR);
        }
        for(long id:ids){
            SysDept deptFeign = managerFeignClient.findDeptFeign(id);
            String deptName = deptFeign.getDeptName();
            managerFeignClient.setClassNull(deptName);
        }
        classesCourseMapper.delete(new QueryWrapper<ClassesCourse>().in("classes_id", ids));
    }
}
